package service

import (
	"one_exam/global"
	"one_exam/internal/model/dao"
)

// 查询手机号是否存在
func CheckUserByMobile(mobile string) (dao.User, bool) {
	var user dao.User
	err := global.DB.Where("mobile = ?", mobile).First(&user).Error
	if err != nil {
		return user, false
	}
	return user, true
}

func FindPriceId(priceId int) (dao.Price, bool) {
	var price dao.Price
	err := global.DB.Where("price_id = ? ", priceId).First(&price).Error
	if err != nil {
		return price, false
	}
	return price, true
}

func List(name string, sizeDate, old int) (goods *[]dao.Goods, err error) {
	//err = global.DB.Where("name like ?", "%"+name+"%").Where("size = ?", sizeDate).Where("old = ?", old).Find(&goods).Error
	//if err != nil {
	//	return nil, err
	//}
	//return goods, nil
	// 构建初始查询条件
	query := global.DB.Where("name like ?", "%"+name+"%")
	if sizeDate > 0 { //如果容量大于0，才加上这个条件
		query = query.Where("size = ?", sizeDate)
	}
	if old > 0 { //如果成色大于0,才加上这个条件
		query = query.Where("old = ?", old)
	}
	err = query.Find(&goods).Error
	if err != nil {
		return nil, err
	}
	//返回查询数据
	return goods, nil

}
func CheckGoodsById(goodsId int) bool {
	var goods dao.Goods
	err := global.DB.Where("id = ?", goodsId).First(&goods).Error
	if err != nil {
		return false
	}
	return true
}
